package com.snatch.snatchmgr.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.utils.DateUtil;
import com.snatch.snatchmgr.dao.ThirdLiveMapper;
import com.snatch.snatchmgr.dao.ThirdMatchMapper;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.entity.db.ThirdMatch;
import com.snatch.snatchmgr.service.CrawlerDataService;
import com.snatch.snatchmgr.system.SystemData;

@Service
public class CrawlerDataServiceImpl implements CrawlerDataService{
	
	@Autowired
	private ThirdMatchMapper thirdMatchMapper;
	
	@Autowired
	private ThirdLiveMapper thirdLiveMapper;
	
	// 日志对象
	private Logger log = Logger.getLogger(MatchDataServiceImpl.class);
	
	@Override
	public void addThirdMatch(ThirdMatch thirdMatch) {
		SystemData.putCounter("addThirdMatch1:");
		//long t=System.currentTimeMillis();
		ThirdMatch temp=thirdMatchMapper.findThirdMatch(thirdMatch.getTarget(),thirdMatch.getLeagueSelfid(),thirdMatch.getMatchSelfid(),thirdMatch.getStartTime());
		//log.info("===>"+thirdMatch.getName()+"第三方赛事查询耗时："+(System.currentTimeMillis()-t));
		if(temp==null){
			//long t1=System.currentTimeMillis();
			//thirdMatch.setId(crawlerMatchGrabDao.getSeq());
			log.info("赛事信息为空，保存到数据库中："+thirdMatch);
			thirdMatchMapper.insert(thirdMatch)	;
			//log.info("===>"+thirdMatch.getName()+"第三方赛事查询耗时："+(System.currentTimeMillis()-t1));
		}else{
			thirdMatch.setId(temp.getId());
			thirdMatch.setNormalMatchId(thirdMatch.getNormalMatchId());
			thirdMatch.setNormalMatchName(thirdMatch.getNormalMatchName());
			thirdMatch.setStartTimestr(DateUtil.format(thirdMatch.getStartTime(), SnatchConstant.MATCH_START_TIME));
		}
		
	}

	@Override
	public void updThirdMatch(ThirdMatch thirdMatch) {
		SystemData.putCounter("updThirdMatch2:");
		thirdMatchMapper.updateByPrimaryKey(thirdMatch);
	}

	@Override
	public void addThirdLive(ThirdLive thirdlive) {
		SystemData.putCounter("addLiveData3:");
		//long t=System.currentTimeMillis();
		ThirdLive temp=thirdLiveMapper.findThirdLive(thirdlive.getThirdMatchId());
		//log.info("===>"+thirdMatch.getName()+"第三方赛事查询耗时："+(System.currentTimeMillis()-t));
		if(temp==null){
			//long t1=System.currentTimeMillis();
			//thirdMatch.setId(crawlerMatchGrabDao.getSeq());
			log.info("赛事信息为空，保存到数据库中："+thirdlive);
			thirdLiveMapper.insert(thirdlive)	;
			//log.info("===>"+thirdMatch.getName()+"第三方赛事查询耗时："+(System.currentTimeMillis()-t1));
		}else{
			thirdlive = temp;
		}
		
	}

	@Override
	public void updThirdLive(ThirdLive thirdlive) {
		SystemData.putCounter("updLiveData4:");
		thirdLiveMapper.updateByPrimaryKey(thirdlive);
	}

	@Override
	public ThirdMatch findThirdMatch(String target,String leagueSelfid,String matchSelfid,Date startTime){
		return thirdMatchMapper.findThirdMatch(target, leagueSelfid, matchSelfid, startTime);
	}

}
